package easypsync.app;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import android.util.Log;

public class SMBTransfer {

	/**
	 * e.g.: smb://volker:aaaa@192.168.178.30/volker/
	 */
	private static String targetHost;
	
	private static String targetUser;

	private static String targetPass;
	
	private static String targetBase;
	
	

	private static String TAG = SMBTransfer.class.getName();

	
	public static void setTargetHost(String targetHost) {
		SMBTransfer.targetHost = targetHost;
	}
	public static void setTargetUser(String targetUser) {
		SMBTransfer.targetUser = targetUser;
	}
	public static void setTargetPass(String targetPass) {
		SMBTransfer.targetPass = targetPass;
	}
	public static void setTargetBase(String targetBase) {
		SMBTransfer.targetBase = targetBase;
	}
	
	private static String getAuthentication() {
		return targetUser != null && targetPass != null 
				? targetUser + ":" + targetPass + "@" 
				: "";
	}
	
	private static String getPath() {
		return "smb://" + getAuthentication() + targetHost + targetBase + "/" ;
	}
	
	public static void transferFile(String source, String target) {
		if (targetHost == null || targetBase == null) 
			return;

		OutputStream os = null;
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
			SmbFile f = new SmbFile(getPath() + target);
			if (f.exists())
				f.delete();
			f.createNewFile();
			os = f.getOutputStream();
			bos = new BufferedOutputStream(os, 8192);
			fis = new FileInputStream(source);
			BufferedInputStream bis = new BufferedInputStream(fis, 8192);
			byte[] buffer = new byte[8192];
			int count;
			// Log.e(TAG, "Now writing to SMB");
			while ((count = bis.read(buffer)) != -1) {
				// Log.e("SMBTransfer", "Writing " + count + " bytes");
				bos.write(buffer, 0, count);
			}
			Log.i(TAG, "Successfully transferred file: " + source);
		} catch (MalformedURLException e) {
			Log.e(TAG, "Exception: ", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "MalformedURLException");
		} catch (SmbException e) {
			Log.w(TAG, "File: " + source + " could not be transferred", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "SmbException");
		} catch (IOException e) {
			Log.w(TAG, "File: " + source + " could not be transferred" + e);
			throw new SyncException("Failed SMB", "Failed copying to share", "IOException");
		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
				}
		}

	}

	/**
	 * 
	 * @param source e.g.: /mnt/sdcard/blub.ta
	 * @param target e.g.: smb://server/newBlub.ta
	 */
	private static void tFile(File source, String target) {
		OutputStream os = null;
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
			SmbFile f = new SmbFile(target);
			if (f.exists()) {
				if (f.length() == source.length()) {
					return;
				} else {
					f.delete();
				}
			}
			
			
			f.createNewFile();
			os = f.getOutputStream();
			bos = new BufferedOutputStream(os/*, size*/);
			fis = new FileInputStream(source);
			BufferedInputStream bis = new BufferedInputStream(fis/*, size*/);
			byte[] buffer = new byte[8096];
			int count;
			// Log.e(TAG, "Now writing to SMB");
			while ((count = bis.read(buffer)) != -1) {
				// Log.e("SMBTransfer", "Writing " + count + " bytes");
				bos.write(buffer, 0, count);
			}
			Log.i(TAG, "Successfully transferred file: " + source);
		} catch (MalformedURLException e) {
			Log.e(TAG, "Exception: ", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "MalformedURLException");
		} catch (SmbException e) {
			Log.w(TAG, "File: " + source + " could not be transferred", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "SmbException");
		} catch (IOException e) {
			Log.w(TAG, "File: " + source + " could not be transferred" + e);
			throw new SyncException("Failed SMB", "Failed copying to share", "IOException");
		} catch (Throwable e) {
			Log.w(TAG, "Anything Else: " + source + " could not be transferred" + e);
			throw new SyncException("Failed SMB", "Failed copying to share", "Throwable");
		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
				}
		}

	}
	
	public static void syncFiles(File imageDirectory, String dir) {
		if (targetHost == null || targetBase == null) 
			return;	
		
		try {
			SmbFile remoteImageDirectoy = new SmbFile(getPath() + dir + "/");
			if (!remoteImageDirectoy.exists()) {
				remoteImageDirectoy.mkdirs();
			}
			
			
			File[] files = imageDirectory.listFiles();
			for (File file: files) {
				if (!file.isDirectory()) {
					tFile(file, remoteImageDirectoy + file.getName());
				}
			}
			
		} catch (MalformedURLException e) {
			Log.e(TAG, "Exception: ", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "MalformedURLException - sync files");
		} catch (SmbException e) {
			Log.w(TAG, "File: " + dir + " could not be created", e);
			throw new SyncException("Failed SMB", "Failed copying to share", "SmbException - sync files");
		}
	}
}
